#ifndef __SDC_EVENT_H__
#define __SDC_EVENT_H__
#ifdef __cplusplus
extern "C" {
#endif

struct sdc_common_head
{
	uint16_t        version;
	uint8_t         url_ver;
#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__)
#if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
	uint8_t         method: 7;
	uint8_t         response: 1;
#elif (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
	uint8_t         response: 1;
	uint8_t         method: 7;
#else
#error "unknown __BYTE_ORDER__"
#endif
#else
#error "don't define __BYTE_ORDER__ or __ORDER_LITTLE_ENDIAN__ or __ORDER_BIG_ENDIAN__"
#endif
	uint16_t        url;
	uint16_t        code;
	uint16_t        head_length;
	uint16_t        trans_id;
	uint32_t        content_length;
};

#define SDC_VERSION            0x5331
#define SDC_URL_HARDWARE_ID    100

#define SDC_METHOD_CREATE       1
#define SDC_METHOD_GET          2
#define SDC_METHOD_UPDATE       3
#define SDC_METHOD_DELETE       4

#define SDC_CODE_200            200
#define SDC_CODE_400            400
#define SDC_CODE_401            401
#define SDC_CODE_403            403
#define SDC_CODE_500            500

#define event_paas_name "/mnt/srvfs/event.paas.sdc"

#define SDC_URL_PAAS_EVENTD_EVENT 0x0000
#define SDC_HEAD_SHM_CACHED_EVENT 0xFFFF

#define SDC_URL_PAAS_EVENTD_SUBSCRIBE_STAT 	0X01
#define SDC_URL_PAAS_EVENTD_PUBLISH_STAT	0X02

typedef struct paas_event {
	char publisher[16];     //发送事件的服务标识，调测使用
	char name[16];          //事件唯一标识，建议同域名定义避免冲突
	uint64_t src_timestamp; //发生时的时间，单位毫秒（CLOCK_MONOTONIC时间）
	uint64_t tran_timestamp;//服务转发的时间，单位毫秒（CLOCK_MONOTONIC时间）
	uint32_t id;            //建议同IP地址一样管理，不同前缀对应事件分类，方便分类订阅。
	uint32_t length;        //事件内容的长度.
	char data[0];
}paas_event_s;

typedef struct paas_shm_cached_event {
    uint64_t addr_phy;
    uint32_t size;
    uint32_t cookie;
}paas_shm_cached_event_s;

/** * filter是订阅条件是一个布尔值表达式，只要表达式返回true（非0）就表示事件匹 配成功
 * * 1. 订阅所有事件: filter = "1";
 * * 2. 基于掩码订阅事件: "event.id & 0xFFFF0000";
 * * 3. 订阅特定值的时间: "event.id in [2,3,9-30]"; *
 * 4. 更复杂的订阅条件: "(event.id & 0x0000FFFF) in [20-30] && event.length > 0"; *
 * 5. 根据事件的内容订阅: "event.body.u32[4] == 5";
 * */ 
struct paas_event_filter {
	char subscriber[16];	// 订阅者的标识, 调测使用
	char name[16];			// 订阅者名字
	char filter[256];
};

struct paas_event_subscribe_stat {
	char name[16];
	uint64_t cnt;
	uint64_t fail_cnt;
};

struct paas_event_publish_stat {
	char name[16];
	uint32_t subscriber_cnt;
	uint64_t cnt;
	uint64_t trans_cnt;		// 成功转发的次数
	uint64_t fail_cnt;		// 转发失败的次数
};

struct sdc_shm_cache {		//shm
    void* addr_virt;		//指向paas_event
    unsigned long addr_phy;
    unsigned int size;		//paas_event head 的大小和data iovec数据的大小  然后将sdc_shm_cache 在/dev/cache中申请共享内存的地址用于拷贝元数据内容
    unsigned int cookie;
};

#define SDC_CACHE_ALLOC _IOR(SDC_FT_CACHE, 0x00, struct sdc_shm_cache)
#define SDC_FT_CACHE 7
#define SDC_CACHE_GETPHY _IOR(SDC_FT_CACHE, 0x00, struct sdc_shm_cache)
#define SDC_CACHE_MMAP _IOR(SDC_FT_CACHE, 0x01, struct sdc_shm_cache)

#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif

#ifdef __cplusplus
}
#endif
#endif /* __SDC_EVENT_H__ */